<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2013, Chongqing Zero Technology Co. Ltd.
 * @link           http://www.xuewl.com
**/
namespace Document\Controller;
use \Admin\Controller\InitController;
define('MODULE_CACHE', DATA_PATH.'caches_model/');
Class PlatformController extends InitController {
      public function _initialize() {
       parent::_initialize();
        $this->db = D('Platform');
    }

    public function index(){
        $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\''.U('add').'\', title:\''.L('posid_add').'\', width:\'500\', height:\'250\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('posid_add'));
        $pagecurr = max(1,I('page',0,'intval')); 
        $pagesize = 10;
        $result = $this->db->order('listorder asc')->page($pagecurr,$pagesize)->select();
        $count = $this->db->count(); 
        $pages = page($count, 10);
        include $this->admin_tpl('platform_list');
    }
    /**
     * 添加数据
     * @param integer $catid [description]
     */
    public function add(){
        if (submitcheck('dosubmit')) {
            $info = $_POST['info'];
            $tmps = $this->db->add_content($info);  
             if($tmps){
                $this->success('操作成功', 'javascript:close_dialog();', 2);
            }else{
                $this->error($this->db->getError());
            }
        } else {           
            $show_header = FALSE;
            include $this->admin_tpl('platform_add');                
        }
    }
    /**
     * 修改平台参数
     * @param   $id 修改记录的ID
     */
    public function update($id = 0){
             if($id){
                $info = $this->db->update_content($id);
                include $this->admin_tpl('platform_update');
            }
            if($_POST['info']){
                $info = $this->db->update_content($_POST['info']);
                if($info)
                    $this->success('操作成功', 'javascript:close_dialog();', 2);
                else
                    $this->error($this->db->getError());
            }       
            $show_header = false;
    }
    /**
     * 删除数据
     */
    public function delete(){
        if($_GET['id']){
            $id = $_GET['id'];
            $result = $this->db->delete_content($id);
            if($result)
               $this->success('操作成功', 'javascript:close_dialog();', 2);
            else
                $this->error('操作失败！');
        }
        if($_POST['aid']) {
            $result = $this->db->delete_content($_POST['aid']);
            if($result)
            {
               $this->success('操作成功', '', 2);
            }else{
                $this->error('操作失败！');
            }
        }   
    }

    /* 平台排序 */
    public function listorder(){
        $listorder = (array) $_POST['listorder'];
        foreach ($listorder as $k => $v) {
            if(!is_numeric($k) || !is_numeric($v)) continue;
            $this->db->where(array('id' => $k))->setField('listorder', $v);
        }
        $this->success('操作成功');
    }

    public function public_select() {
        $pagecurr = max(1,I('page',0,'intval')); 
        $pagesize = 10;
        $form = new \Common\Library\form();
        $show_header = FALSE;
        $infos = $this->db->order('listorder ASC')->page($pagecurr,$pagesize)->select();
        $count = $this->db->count(); 
        $pages = page($count, 10);
        include $this->admin_tpl('platform_select');
    }
    /**
     * 验证平台名称是否重复
     */
    public function confirmation(){
        $name = I('name');
        $result = $this->db->where(array('name'=>$name))->find();
        if($result){
            echo '2';
        }else{
            echo '1';
        }
    }
    
}